﻿using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.DataBaseStores;
using System;
using System.Linq;
using System.Text;
using Ldt.DataEngine.DataFlowParse.Model;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.DataBaseStores;
using LdtDataEngin.ModuleDetail.AnalysisModule.PersonnelAnalysis.PersonnelModel;
using System.IO;
using LdtDataEngin.ModuleDetail.AnalysisModule.PersonnelAnalysis;
using LdtDataEngin.ModuleDetail.AnalysisModule.PersonnelAnalysis.PersonnelModel.Personnel;

namespace LdtDataEngin.ModuleDetail.PersonnelAnalysis.PersonnelModel
{
    /// <summary>
    /// 人员实体提取模块
    /// 输入信息：格式化后的文本内容===>DataContext.DyData_Analysis
    ///          人员实体提取的配置信息===>DataContext.DataLayerSysStores
    /// 输出信息：人员实体数据===>DataContext.DyData_Analysis.Anjianshitis
    ///          实体角色数据===>DataContext.DataLayerAns.Anjianshitijueses
    /// </summary>
    public class PersonnelExtractionAssemble : AssermBase
    {
        public PersonnelExtractionAssemble(DataContext context, string moudleName)
        {
            dataContext = context;
            base.MoudleName = moudleName;
        }
        public void Extrat()
        {
            var configModel = dataContext.DataLayerSys.GetConfigModel(MoudleName);
            if (configModel != null)
            {
                try
                {
                    ConfigModel configDetail = new ConfigModel();
                    if (configModel.ConfigDetail != null)
                    {
                        var convertTabel = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "transformationconfig");
                        if (convertTabel != null && !string.IsNullOrEmpty(dataContext.DataLayerAns.BcXML))
                        {
                            string cofigxml = "";

                            ConfigModel speakerExtractConfig = new ConfigModel();
                            speakerExtractConfig = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "speakerextract");
                            if (speakerExtractConfig != null)
                            {
                                cofigxml = speakerExtractConfig?.ConfigContent;
                                if (dataContext.DataLayerAns.Duanluos != null && dataContext.DataLayerAns.Duanluos.Count > 0)
                                {
                                    foreach (var duanluo in dataContext.DataLayerAns.Duanluos)
                                    {
                                        dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong = dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong + duanluo.Duanluoneirong;

                                    }

                                }
                            }
                            PersonelExtratSupplement personelExtratSupplement = new PersonelExtratSupplement() { datacontext = dataContext,xmlconfig= cofigxml };
                            personelExtratSupplement.Extrat(convertTabel.ConfigContent);
                        }
                        else
                        {
                            //获取机器学习实体提取配置
                            configDetail = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "mllogicalsource");
                            if (configDetail != null)
                            {
                                string xml = "";
                                //获取文书的发言人提取配置
                                ConfigModel speakerExtractConfig = new ConfigModel();
                                speakerExtractConfig = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "speakerextract");
                                xml = speakerExtractConfig?.ConfigContent;
                                PersonnelStart personnelStart = new PersonnelStart(dataContext, MoudleName) { xmlConfig = xml };
                                personnelStart.Extract();
                            }
                            else
                            {
                                //获取实体提取正则配置
                                configDetail = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "regexparseperson");
                                string xml = "";
                                string cofigxml = "";
                                if (configDetail != null)
                                {
                                    ConfigModel speakerExtractConfig = new ConfigModel();
                                    speakerExtractConfig = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "speakerextract");
                                    if (speakerExtractConfig != null)
                                    {
                                        cofigxml = speakerExtractConfig?.ConfigContent;
                                    }
                                    xml = configDetail.ConfigContent;
                                    PersonRegexSimple personnelStart = new PersonRegexSimple() { XmlConfig = xml, dataContext = dataContext, config = cofigxml };
                                    personnelStart.Extract();

                                }
                                else
                                {
                                    PersonnelStart personnelStart = new PersonnelStart(dataContext, MoudleName) { xmlConfig = xml };
                                    personnelStart.Extract();
                                }

                            }
                        }


                    }
                }
                catch (Exception ex)
                {
                    dataContext.InteractiveShow.LogShow("机器学习调用分人员失败");
                }
            }
        }
    }
}
